home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_022 / pemacs / main.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  25KB  |  633 lines

  1. /*
  2.  * This program is in public domain; written by Dave G. Conroy.
  3.  * This file contains the main driving routine, and some keyboard processing
  4.  * code, for the MicroEMACS screen editor.
  5.  *
  6.  * REVISION HISTORY:
  7.  *
  8.  * 1.0  Steve Wilhite, 30-Nov-85
  9.  *      - Removed the old LK201 and VT100 logic. Added code to support the
  10.  *        DEC Rainbow keyboard (which is a LK201 layout) using the the Level
  11.  *        1 Console In ROM INT. See "rainbow.h" for the function key definitions.
  12.  *
  13.  * 2.0  George Jones, 12-Dec-85
  14.  *      - Ported to Amiga.
  15.  */
  16. #include        <stdio.h>
  17. #include        "ed.h"
  18.  
  19. #if     VMS
  20. #include        <ssdef.h>
  21. #define GOOD    (SS$_NORMAL)
  22. #endif
  23.  
  24. #ifndef GOOD
  25. #define GOOD    0
  26. #endif
  27.  
  28. int     currow;                         /* Working cursor row           */
  29. int     curcol;                         /* Working cursor column        */
  30. int     fillcol;                        /* Current fill column          */
  31. int     wordwrap;                       /* Word wrap flag        */
  32. int     thisflag;                       /* Flags, this command          */
  33. int     lastflag;                       /* Flags, last command          */
  34. int     curgoal;                        /* Goal column                  */
  35. BUFFER  *curbp;                         /* Current buffer               */
  36. WINDOW  *curwp;                         /* Current window               */
  37. BUFFER  *bheadp;                        /* BUFFER listhead              */
  38. WINDOW  *wheadp;                        /* WINDOW listhead              */
  39. BUFFER  *blistp;                        /* Buffer list BUFFER           */
  40. BUFFER  *prev_bp;                       /* Previous Buffer               */
  41. short   kbdm[NKBDM] = {CTLX|')'};       /* Macro                        */
  42. short   *kbdmip;                        /* Input  for above             */
  43. short   *kbdmop;                        /* Output for above             */
  44. char    pat[NPAT];                      /* Pattern                      */
  45.  
  46. typedef struct  {
  47.         short   k_code;                 /* Key code                     */
  48.         int     (*k_fp)();              /* Routine to handle it         */
  49. }       KEYTAB;
  50.  
  51. extern  int     ctrlg();                /* Abort out of things          */
  52. extern  int     quit();                 /* Quit                         */
  53. extern  int     ctlxlp();               /* Begin macro                  */
  54. extern  int     ctlxrp();               /* End macro                    */
  55. extern  int     ctlxe();                /* Execute macro                */
  56. extern  int     fileread();             /* Get a file, read only        */
  57. extern  int     filevisit();            /* Get a file, read write       */
  58. extern  int     filewrite();            /* Write a file                 */
  59. extern  int     filesave();             /* Save current file            */
  60. extern  int     filename();             /* Adjust file name             */
  61. extern  int     getccol();              /* Get current column           */
  62. extern  int     gotobol();              /* Move to start of line        */
  63. extern  int     forwchar();             /* Move forward by characters   */
  64. extern  int     gotoeol();              /* Move to end of line          */
  65. extern  int     backchar();             /* Move backward by characters  */
  66. extern  int     forwline();             /* Move forward by lines        */
  67. extern  int     backline();             /* Move backward by lines       */
  68. extern  int     forwpage();             /* Move forward by pages        */
  69. extern  int     backpage();             /* Move backward by pages       */
  70. extern  int     gotobob();              /* Move to start of buffer      */
  71. extern  int     gotoeob();              /* Move to end of buffer        */
  72. extern  int     setfillcol();           /* Set fill column.             */
  73. extern  int     togglewordwrap();       /* Toggle word wrap        */
  74. extern  int     setmark();              /* Set mark                     */
  75. extern  int     swapmark();             /* Swap "." and mark            */
  76. extern  int     forwsearch();           /* Search forward               */
  77. extern  int     backsearch();           /* Search backwards             */
  78. extern  int     qreplace();             /* Query Replace        */
  79. extern  int     showcpos();             /* Show the cursor position     */
  80. extern  int     nextwind();             /* Move to the next window      */
  81. extern  int     prevwind();             /* Move to the previous window  */
  82. extern  int     onlywind();             /* Make current window only one */
  83. extern  int     splitwind();            /* Split current window         */
  84. extern  int     mvdnwind();             /* Move window down             */
  85. extern  int     mvupwind();             /* Move window up               */
  86. extern  int     enlargewind();          /* Enlarge display window.      */
  87. extern  int     shrinkwind();           /* Shrink window.               */
  88. extern  int     listbuffers();          /* Display list of buffers      */
  89. extern  int     usebuffer();            /* Switch a window to a buffer  */
  90. extern  int     killbuffer();           /* Make a buffer go away.       */
  91. extern  int     reposition();           /* Reposition window            */
  92. extern  int     refresh();              /* Refresh the screen           */
  93. extern  int     twiddle();              /* Twiddle characters           */
  94. extern  int     tab();                  /* Insert tab                   */
  95. extern  int     newline();              /* Insert CR-LF                 */
  96. extern  int     indent();               /* Insert CR-LF, then indent    */
  97. extern  int     openline();             /* Open up a blank line         */
  98. extern  int     deblank();              /* Delete blank lines           */
  99. extern  int     quote();                /* Insert literal               */
  100. extern  int     backword();             /* Backup by words              */
  101. extern  int     forwword();             /* Advance by words             */
  102. extern  int     forwdel();              /* Forward delete               */
  103. extern  int     backdel();              /* Backward delete              */
  104. extern  int     kill();                 /* Kill forward                 */
  105. extern  int     yank();                 /* Yank back from killbuffer.   */
  106. extern  int     upperword();            /* Upper case word.             */
  107. extern  int     lowerword();            /* Lower case word.             */
  108. extern  int     upperregion();          /* Upper case region.           */
  109. extern  int     lowerregion();          /* Lower case region.           */
  110. extern  int     capword();              /* Initial capitalize word.     */
  111. extern  int     delfword();             /* Delete forward word.         */
  112. extern  int     delbword();             /* Delete backward word.        */
  113. extern  int     killregion();           /* Kill region.                 */
  114. extern  int     copyregion();           /* Copy region to kill buffer.  */
  115. extern  int     spawncli();             /* Run CLI in a subjob.         */
  116. extern  int     spawn();                /* Run a command in a subjob.   */
  117. extern  int     quickexit();            /* low keystroke style exit.    */
  118. extern int    parafill();        /* fill paragraph */
  119.  
  120. /*
  121.  * Command table.
  122.  * This table  is *roughly* in ASCII order, left to right across the
  123.  * characters of the command. This expains the funny location of the
  124.  * control-X commands.
  125.  */
  126. KEYTAB  keytab[] = {
  127.         CTRL|'@',               &setmark,
  128.         CTRL|'A',               &gotobol,
  129.         CTRL|'B',               &backchar,
  130.         CTRL|'C',               &spawncli,      /* Run CLI in subjob.   */
  131.         CTRL|'D',               &forwdel,
  132.         CTRL|'E',               &gotoeol,
  133.         CTRL|'F',               &forwchar,
  134.         CTRL|'G',               &ctrlg,
  135.         CTRL|'H',               &backdel,
  136.         CTRL|'I',               &tab,
  137.         CTRL|'J',               &indent,
  138.         CTRL|'K',               &kill,
  139.         CTRL|'L',               &refresh,
  140.         CTRL|'M',               &newline,
  141.         CTRL|'N',               &forwline,
  142.         CTRL|'O',               &openline,
  143.         CTRL|'P',               &backline,
  144.         CTRL|'Q',               "e,         /* Often unreachable    */
  145.         CTRL|'R',               &backsearch,
  146.         CTRL|'S',               &forwsearch,    /* Often unreachable    */
  147.         CTRL|'T',               &twiddle,
  148.         CTRL|'V',               &forwpage,
  149.         CTRL|'W',               &killregion,
  150.         CTRL|'Y',               &yank,
  151.         CTRL|'Z',               &quickexit,     /* quick save and exit  */
  152.         CTLX|CTRL|'B',          &listbuffers,
  153.         CTLX|CTRL|'C',          &quit,          /* Hard quit.           */
  154. /*        CTLX|CTRL|'F',          &filename,*/
  155.         CTLX|CTRL|'F',          &filevisit,
  156.         CTLX|CTRL|'L',          &lowerregion,
  157.         CTLX|CTRL|'O',          &deblank,
  158.         CTLX|CTRL|'N',          &mvdnwind,
  159.         CTLX|CTRL|'P',          &mvupwind,
  160.         CTLX|CTRL|'R',          &fileread,
  161.         CTLX|CTRL|'S',          &filesave,      /* Often unreachable    */
  162.         CTLX|CTRL|'U',          &upperregion,
  163.         CTLX|CTRL|'V',          &filevisit,
  164.         CTLX|CTRL|'W',          &filewrite,
  165.         CTLX|CTRL|'X',          &swapmark,
  166.         CTLX|CTRL|'Z',          &shrinkwind,
  167.         CTLX|'!',               &spawn,         /* Run 1 command.       */
  168.         CTLX|'=',               &showcpos,
  169.         CTLX|'(',               &ctlxlp,
  170.         CTLX|')',               &ctlxrp,
  171.         CTLX|'1',               &onlywind,
  172.         CTLX|'2',               &splitwind,
  173.         CTLX|'B',               &usebuffer,
  174.         CTLX|'E',               &ctlxe,
  175.         CTLX|'F',               &setfillcol,
  176.         CTLX|'K',               &killbuffer,
  177.         CTLX|'N',               &nextwind,
  178.         CTLX|'P',               &prevwind,
  179.         CTLX|'T',               &togglewordwrap,
  180.         CTLX|'Z',               &enlargewind,
  181.         META|CTRL|'H',          &delbword,
  182.         META|'!',               &reposition,
  183.         META|'.',               &setmark,
  184.         META|'>',               &gotoeob,
  185.         META|'<',               &gotobob,
  186.         META|'B',               &backword,
  187.         META|'C',               &capword,
  188.         META|'D',               &delfword,
  189.         META|'F',               &forwword,
  190.         META|'L',               &lowerword,
  191.         META|'Q',               ¶fill,
  192.         META|'U',               &upperword,
  193.         META|'V',               &backpage,
  194.         META|'W',               ©region,
  195.     META|'%',        &qreplace,
  196.         META|0x7F,              &delbword,
  197.         0x7F,                   &backdel
  198. };
  199.  
  200. #define NKEYTAB (sizeof(keytab)/sizeof(keytab[0]))
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209. #if RAINBOW
  210.  
  211. #include "rainbow.h"
  212.  
  213. /*
  214.  * Mapping table from the LK201 function keys to the internal EMACS character.
  215.  */
  216.  
  217. short lk_map[][2] = {
  218.         Up_Key,                         CTRL+'P',
  219.         Down_Key,                       CTRL+'N',
  220.         Left_Key,                       CTRL+'B',
  221.         Right_Key,                      CTRL+'F',
  222.         Shift+Left_Key,                 META+'B',
  223.         Shift+Right_Key,                META+'F',
  224.         Control+Left_Key,               CTRL+'A',
  225.         Control+Right_Key,              CTRL+'E',
  226.         Prev_Scr_Key,                   META+'V',
  227.         Next_Scr_Key,                   CTRL+'V',
  228.         Shift+Up_Key,                   META+'<',
  229.         Shift+Down_Key,                 META+'>',
  230.         Cancel_Key,                     CTRL+'G',
  231.         Find_Key,                       CTRL+'S',
  232.         Shift+Find_Key,                 CTRL+'R',
  233.         Insert_Key,                     CTRL+'Y',
  234.         Options_Key,                    CTRL+'D',
  235.         Shift+Options_Key,              META+'D',
  236.         Remove_Key,                     CTRL+'W',
  237.         Shift+Remove_Key,               META+'W',
  238.         Select_Key,                     CTRL+'@',
  239.         Shift+Select_Key,               CTLX+CTRL+'X',
  240.         Interrupt_Key,                  CTRL+'U',
  241.         Keypad_PF2,                     META+'L',
  242.         Keypad_PF3,                     META+'C',
  243.         Keypad_PF4,                     META+'U',
  244.         Shift+Keypad_PF2,               CTLX+CTRL+'L',
  245.         Shift+Keypad_PF4,               CTLX+CTRL+'U',
  246.         Keypad_1,                       CTLX+'1',
  247.         Keypad_2,                       CTLX+'2',
  248.         Do_Key,                         CTLX+'E',
  249.         Keypad_4,                       CTLX+CTRL+'B',
  250.         Keypad_5,                       CTLX+'B',
  251.         Keypad_6,                       CTLX+'K',
  252.         Resume_Key,                     META+'!',
  253.         Control+Next_Scr_Key,           CTLX+'N',
  254.         Control+Prev_Scr_Key,           CTLX+'P',
  255.         Control+Up_Key,                 CTLX+CTRL+'P',
  256.         Control+Down_Key,               CTLX+CTRL+'N',
  257.         Help_Key,                       CTLX+'=',
  258.         Shift+Do_Key,                   CTLX+'(',
  259.         Control+Do_Key,                 CTLX+')',
  260.         Keypad_0,                       CTLX+'Z',
  261.         Shift+Keypad_0,                 CTLX+CTRL+'Z',
  262.         Main_Scr_Key,                   CTRL+'C',
  263.         Keypad_Enter,                   CTLX+'!',
  264.         Exit_Key,                       CTLX+CTRL+'C',
  265.         Shift+Exit_Key,                 CTRL+'Z'
  266.         };
  267.  
  268. #define lk_map_size     (sizeof(lk_map)/2)
  269.  
  270. #endif
  271.  
  272. #if AMIGA
  273. #define CSI 0x9B
  274. #endif
  275.  
  276. main(argc, argv)
  277. char    *argv[];
  278. {
  279.         register int    c;
  280.         register int    f;
  281.         register int    n;
  282.         char            bname[NBUFN];
  283.     int    mflag;
  284.  
  285.         strcpy(bname, "main");                  /* Work out the name of */
  286.         if (argc > 1)                           /* the default buffer.  */
  287.                 makename(bname, argv[1]);
  288.         edinit(bname);                          /* Buffers, windows.    */
  289.         vtinit();                               /* Displays.            */
  290.     fillcol = term.t_ncol -2;
  291.  
  292.         if (argc > 1) {
  293.                 update();                       /* You have to update   */
  294.                 readin(argv[1]);                /* in case "[New file]" */
  295.         }
  296.         lastflag = 0;                           /* Fake last flags.     */
  297. loop:
  298.         update();                               /* Fix up the screen    */
  299.         c = getkey();
  300.         if (mpresf != FALSE) {
  301.                 mlerase();
  302.                 update();
  303. #if AMIGA
  304.         /* the its action for space interacts badly with mouse
  305.          * interaction, so we skip it for the amiga.
  306.          */
  307. #else
  308.                 if (c == ' ')                   /* ITS EMACS does this  */
  309.                         goto loop;
  310. #endif
  311.         }
  312.         f = FALSE;
  313.         n = 1;
  314.         if (c == (CTRL|'U')) {                  /* ^U, start argument   */
  315.                 f = TRUE;
  316.                 n = 4;                          /* with argument of 4 */
  317.                 mflag = 0;                      /* that can be discarded. */
  318.                 mlwrite("Arg: 4");
  319.                 while (((c=getkey()) >='0' && c<='9') || c==(CTRL|'U')
  320.         || c=='-') {
  321.                         if (c == (CTRL|'U'))
  322.                                 n = n*4;
  323.                         /*
  324.                          * If dash, and start of argument string, set arg.
  325.                          * to -1.  Otherwise, insert it.
  326.                          */
  327.                         else if (c == '-') {
  328.                                 if (mflag)
  329.                                         break;
  330.                                 n = 0;
  331.                                 mflag = -1;
  332.                         }
  333.                         /*
  334.                          * If first digit entered, replace previous argument
  335.                          * with digit and set sign.  Otherwise, append to arg.
  336.                          */
  337.                         else {
  338.                                 if (!mflag) {
  339.                                         n = 0;
  340.                                         mflag = 1;
  341.                                 }
  342.                                 n = 10*n + c - '0';
  343.                         }
  344.                         mlwrite("Arg: %d", (mflag >=0) ? n : (n ? -n : -1));
  345.                 }
  346.                 /*
  347.                  * Make arguments preceded by a minus sign negative and change
  348.                  * the special argument "^U -" to an effective "^U -1".
  349.                  */
  350.                 if (mflag == -1) {
  351.                         if (n == 0)
  352.                                 n++;
  353.                         n = -n;
  354.                 }
  355.         }
  356.         if (c == (CTRL|'X'))                    /* ^X is a prefix       */
  357.                 c = CTLX | getctl();
  358. #if AMIGA
  359.     if( (c&0xFF) == CSI) {
  360.         int execute;
  361.         if( kbdmip != NULL) { /* collecting macro */
  362.             execute = FALSE; /* don't actually do it */
  363.                         ctrlg(FALSE, 0);
  364.         } else
  365.             execute = TRUE;
  366.         mouse_handle_event( execute, f, n);
  367.         goto loop;
  368.     }
  369. #endif
  370.         if (kbdmip != NULL) {                   /* Save macro strokes.  */
  371.                 if (c!=(CTLX|')') && kbdmip>&kbdm[NKBDM-6]) {
  372.                         ctrlg(FALSE, 0);
  373.                         goto loop;
  374.                 }
  375.                 if (f != FALSE) {
  376.                         *kbdmip++ = (CTRL|'U');
  377.                         *kbdmip++ = n;
  378.                 }
  379.                 *kbdmip++ = c;
  380.         }
  381.         execute(c, f, n);                       /* Do it.               */
  382.         goto loop;
  383. }
  384.  
  385. /*
  386.  * Initialize all of the buffers and windows. The buffer name is passed down
  387.  * as an argument, because the main routine may have been told to read in a
  388.  * file by default, and we want the buffer name to be right.
  389.  */
  390. edinit(bname)
  391. char    bname[];
  392. {
  393.         register BUFFER *bp;
  394.         register WINDOW *wp;
  395.  
  396.         bp = bfind(bname, TRUE, 0);             /* First buffer         */
  397.         blistp = bfind("[List]", TRUE, BFTEMP); /* Buffer list buffer   */
  398.         wp = (WINDOW *) malloc(sizeof(WINDOW)); /* First window         */
  399.         if (bp==NULL || wp==NULL || blistp==NULL)
  400.                 exit(1);
  401.         curbp  = bp;                            /* Make this current    */
  402.         wheadp = wp;
  403.         curwp  = wp;
  404.         wp->w_wndp  = NULL;                     /* Initialize window    */
  405.         wp->w_bufp  = bp;
  406.         bp->b_nwnd  = 1;                        /* Displayed.           */
  407.         wp->w_linep = bp->b_linep;
  408.         wp->w_dotp  = bp->b_linep;
  409.         wp->w_doto  = 0;
  410.         wp->w_markp = NULL;
  411.         wp->w_marko = 0;
  412.         wp->w_toprow = 0;
  413.         wp->w_ntrows = term.t_nrow-1;           /* "-1" for mode line.  */
  414.         wp->w_force = 0;
  415.         wp->w_flag  = WFMODE|WFHARD;            /* Full.                */
  416. }
  417.         
  418. /*
  419.  * This is the general command execution routine. It handles the fake binding
  420.  * of all the keys to "self-insert". It also clears out the "thisflag" word,
  421.  * and arranges to move it to the "lastflag", so that the next command can
  422.  * look at it. Return the status of command.
  423.  */
  424. execute(c, f, n)
  425. {
  426.         register KEYTAB *ktp;
  427.         register int    status;
  428.  
  429.         if ((c>=0x20 && c<=0x7E)                /* Self inserting.      */
  430.         ||  (c>=0xA0 && c<=0xFE)) {
  431.         /*
  432.          * If word wrap is true, the argument is non-
  433.          * negative, and we are now past fill column, perform word wrap.
  434.              */
  435.             if (wordwrap && n>=0 && getccol(FALSE) > fillcol)
  436.                 wrapword();
  437.  
  438.                 if (n <= 0) {                   /* Fenceposts.          */
  439.                         lastflag = 0;
  440.                         return (n<0 ? FALSE : TRUE);
  441.                 }
  442.                 thisflag = 0;                   /* For the future.      */
  443.                 status   = linsert(n, c);
  444.                 lastflag = thisflag;
  445.                 return (status);
  446.         }
  447.  
  448.         ktp = &keytab[0];                       /* Look in key table.   */
  449.         while (ktp < &keytab[NKEYTAB]) {
  450.                 if (ktp->k_code == c) {
  451.                         thisflag = 0;
  452.                         status   = (*ktp->k_fp)(f, n);
  453.                         lastflag = thisflag;
  454.                         return (status);
  455.                 }
  456.                 ++ktp;
  457.         }
  458.  
  459.         lastflag = 0;                           /* Fake last flags.     */
  460.         return (FALSE);
  461. }
  462.  
  463. /*
  464.  * Read in a key.
  465.  * Do the standard keyboard preprocessing. Convert the keys to the internal
  466.  * character set.
  467.  */
  468. getkey()
  469. {
  470.         register int    c;
  471.  
  472.         c = (*term.t_getchar)();
  473.  
  474. #if RAINBOW
  475.  
  476.         if (c & Function_Key)
  477.                 {
  478.                 int i;
  479.  
  480.                 for (i = 0; i < lk_map_size; i++)
  481.                         if (c == lk_map[i][0])
  482.                                 return lk_map[i][1];
  483.                 }
  484.         else if (c == Shift + 015) return CTRL | 'J';
  485.         else if (c == Shift + 0x7F) return META | 0x7F;
  486. #endif
  487.  
  488.         if (c == METACH) {                      /* Apply M- prefix      */
  489.                 c = getctl();
  490.                 return (META | c);
  491.         }
  492.  
  493.         if (c>=0x00 && c<=0x1F)                 /* C0 control -> C-     */
  494.                 c = CTRL | (c+'@');
  495.  
  496. #if AMIGA
  497.     else if(( c & 0x80) && ((c&0xFF) != CSI)) { /* Meta-shifted char */
  498.         c &= 0x7F;
  499.             if (c>='a' && c<='z')        /* Force to upper       */
  500.                     c -= 0x20;
  501.         c |= META;
  502.     }
  503. #endif
  504.         return (c);
  505. }
  506.  
  507. /*
  508.  * Get a key.
  509.  * Apply control modifications to the read key.
  510.  */
  511. getctl()
  512. {
  513.         register int    c;
  514.  
  515.         c = (*term.t_getchar)();
  516.         if (c>='a' && c<='z')                   /* Force to upper       */
  517.                 c -= 0x20;
  518.         if (c>=0x00 && c<=0x1F)                 /* C0 control -> C-     */
  519.                 c = CTRL | (c+'@');
  520.         return (c);
  521. }
  522.  
  523. /*
  524.  * Fancy quit command, as implemented by Norm. If the current buffer has
  525.  * changed do a write current buffer and exit emacs, otherwise simply exit.
  526.  */
  527. quickexit(f, n)
  528. {
  529.         if ((curbp->b_flag&BFCHG) != 0          /* Changed.             */
  530.         && (curbp->b_flag&BFTEMP) == 0)         /* Real.                */
  531.                 filesave(f, n);
  532.         quit(f, n);                             /* conditionally quit   */
  533. }
  534.  
  535. /*
  536.  * Quit command. If an argument, always quit. Otherwise confirm if a buffer
  537.  * has been changed and not written out. Normally bound to "C-X C-C".
  538.  */
  539. quit(f, n)
  540. {
  541.         register int    s;
  542.  
  543.         if (f != FALSE                          /* Argument forces it.  */
  544.         || anycb() == FALSE                     /* All buffers clean.   */
  545.         || (s=mlyesno("Modified buffers exist.  Quit")) == TRUE) {
  546.                            /* User says it's OK.   */
  547.                 vttidy();
  548.                 exit(GOOD);
  549.         }
  550.         return (s);
  551. }
  552.  
  553. /*
  554.  * Begin a keyboard macro.
  555.  * Error if not at the top level in keyboard processing. Set up variables and
  556.  * return.
  557.  */
  558. ctlxlp(f, n)
  559. {
  560.         if (kbdmip!=NULL || kbdmop!=NULL) {
  561.                 mlwrite("Not now");
  562.                 return (FALSE);
  563.         }
  564.         mlwrite("[Start macro]");
  565.         kbdmip = &kbdm[0];
  566.         return (TRUE);
  567. }
  568.  
  569. /*
  570.  * End keyboard macro. Check for the same limit conditions as the above
  571.  * routine. Set up the variables and return to the caller.
  572.  */
  573. ctlxrp(f, n)
  574. {
  575.         if (kbdmip == NULL) {
  576.                 mlwrite("Not now");
  577.                 return (FALSE);
  578.         }
  579.         mlwrite("[End macro]");
  580.         kbdmip = NULL;
  581.         return (TRUE);
  582. }
  583.  
  584. /*
  585.  * Execute a macro.
  586.  * The command argument is the number of times to loop. Quit as soon as a
  587.  * command gets an error. Return TRUE if all ok, else FALSE.
  588.  */
  589. ctlxe(f, n)
  590. {
  591.         register int    c;
  592.         register int    af;
  593.         register int    an;
  594.         register int    s;
  595.  
  596.         if (kbdmip!=NULL || kbdmop!=NULL) {
  597.                 mlwrite("Not now");
  598.                 return (FALSE);
  599.         }
  600.         if (n <= 0) 
  601.                 return (TRUE);
  602.         do {
  603.                 kbdmop = &kbdm[0];
  604.                 do {
  605.                         af = FALSE;
  606.                         an = 1;
  607.                         if ((c = *kbdmop++) == (CTRL|'U')) {
  608.                                 af = TRUE;
  609.                                 an = *kbdmop++;
  610.                                 c  = *kbdmop++;
  611.                         }
  612.                         s = TRUE;
  613.                 } while (c!=(CTLX|')') && (s=execute(c, af, an))==TRUE);
  614.                 kbdmop = NULL;
  615.         } while (s==TRUE && --n);
  616.         return (s);
  617. }
  618.  
  619. /*
  620.  * Abort.
  621.  * Beep the beeper. Kill off any keyboard macro, etc., that is in progress.
  622.  * Sometimes called as a routine, to do general aborting of stuff.
  623.  */
  624. ctrlg(f, n)
  625. {
  626.         (*term.t_beep)();
  627.         if (kbdmip != NULL) {
  628.                 kbdm[0] = (CTLX|')');
  629.                 kbdmip  = NULL;
  630.         }
  631.         return (ABORT);
  632. }
  633.